$(function () {
    layui.use(['laydate', 'laypage', 'form'], function () {
            let laypage = layui.laypage;
            //分页完整功能
            laypage.render({
                elem: 'demo7',
                count: getNum(),
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                jump: function (obj) {
                   let pageDatas ={"count":obj.count,"curr":obj.curr,"pages":obj.pages,"limit":obj.limit};

                    $.ajax({
                        url: "http://localhost:11111/api/k/back/user/allUser",
                        dataType: "json",
                        // headers:{'username':localStorage.getItem("loginUser")},
                       // headers:{"token":"Bearer " +localStorage.getItem("token")},
                        headers:{"Authorization":"Bearer "+localStorage.getItem("token")},
                    //  headers:{'token':getCookie("keji_token"),'user':getCookie("keji_token")},
                        type: "post",
                        contentType:"application/json",
                        crossDomain:true,
                        data: JSON.stringify(pageDatas),
                        success: function (res) {
                            var html;
                            console.log("用户分页返回数据",res)
                            console.log("用户分页返回数据1",res[1])
                            for (var i = 0; i < res.data.length; i++) {
                                html += ' <tr>\n' +
                                    '                                    <td>\n' +
                                    '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
                                    '                                    </td>\n' +
                                    '                                    <td>' + res.data[i].id + '</td>\n' +
                                    '                                    <td>' + res.data[i].username + '</td>\n' +
                                    '                                    <td>' + res.data[i].sex + '</td>\n' +
                                    '                                    <td>' + res.data[i].phone + '</td>\n' +
                                    // '                                    <td>' + res.data[i].address + '</td>\n' +
                                    '                                    <td class="td-status">\n' +
                                    '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (res.data[i].enable == 1 ? "" : "layui-btn-disabled") + ' ">' + (res.data[i].enable == 1 ? "启用" : "停用") + '</span></td>\n' +
                                    '                                    <td class="td-manage">\n' +
                                    '                                      <a onclick="member_stop(this,' + res.data[i].id + ')" href="javascript:;"  title=' + (res.data[i].enable == 1 ? "停用" : "启用") + '\>\n' +
                                    '                                        <i class="layui-icon">' + (res.data[i].enable == 1 ? "&#xe601;" : "&#xe62f;") + '</i>\n' +
                                    '                                      </a>\n' +
                                    '                                      <a title="编辑"  onclick="sendId(' + res.data[i].id + ')"href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                      <a onclick="alertPass(' + res.data[i].id + ')" title="修改密码" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe631;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                      <a title="删除" onclick="memberdel(this,' + res.data[i].id + ')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                    </td>\n' +
                                    '                                  </tr>'

                            }
                            $("#userInfo").html(html);

                        }
                    })
                }
            })
        }
    )
})


//得到记录条数
function getNum() {
    let count = 0;
    $.ajax({
        url:"http://localhost:11111/api/k/back/user/count",
        dataType:"json",
        headers:{"Authorization":"Bearer "+localStorage.getItem("token")},
        type:"get",
        async:false,//关闭异步
        success:function (res) {
            // console.log("分页返回值：",res.data)
            count = res.data;

        }
    })
    return count;
}

//修改密码
function alertPass(id) {
    sessionStorage.setItem("id", id);
    xadmin.open('修改密码', '../../html/user/member-password.html', 600, 400)
}


//编辑普通信息

function sendId(id) {
    layui.use(['layer'], function () {
        let layer = layui.layer;
        xadmin.open('编辑', '../../html/user/member-edit.html', 600, 400);
        console.log("id值", id);

        sessionStorage.setItem("id", id);

    })
}
//删除某一行用户
function memberdel(obj, id) {
    let user = {"id":id};
    layui.use(['layer'], function () {
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "http://localhost:11111/api/k/back/user/delete",
                type: "post",
                dataType: "json",
                headers:{"Authorization":"Bearer "+localStorage.getItem("token")},
                data:JSON.stringify(user),
                contentType: 'application/json',//设置请求头表示发送json数据
                success: function (res) {
                    if (res.msg == "success") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('删除失败!', {icon: 1, time: 1000});
                    }


                }
            })
        });
    })
}

/*用户-停用*/
function member_stop(obj, id) {
    layer.confirm('确认要改变可用状态吗？', function (index) {
        sessionStorage.setItem("id", id);
        value = {"id": id, "enable": 1}
        /*如果title为启用，表示该数据是停用的*/
        if ($(obj).attr('title') == '启用') {
            $.ajax({
                url:"http://localhost:11111/api/k/back/user/update",
                type:"post",
                headers:{"Authorization":"Bearer "+localStorage.getItem("token")},
                data:JSON.stringify(value),
                contentType:"application/json",
                dataType:"json",
                success: function () {


                    $(obj).attr('title', '停用')
                    $(obj).find('i').html('&#xe601;');

                    $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('启用');
                    layer.msg('已启用!', {icon: 6, time: 1000});
                }

            })

        } else {
            value.enable = 0;
            $.ajax({
                url:"http://localhost:8080/back/user/update",
                type:"post",
                headers:{"Authorization":"Bearer "+localStorage.getItem("token")},
                data:JSON.stringify(value),
                contentType:"application/json",
                dataType:"json",
                success: function () {
                    //发异步把用户状态进行更改
                    $(obj).attr('title', '启用')
                    $(obj).find('i').html('&#xe62f;');

                    $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('停用');
                    layer.msg('已停用!', {icon: 5, time: 1000});
                }

            })

        }

    });
}
function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function clearCookie(name) {
    setCookie(name, "", -1);
}
function clearAllCookie() {
    var date=new Date();
    date.setTime(date.getTime()-10000);
    var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
    console.log("需要删除的cookie名字："+keys);
    if (keys) {
        for (var i =  keys.length; i--;)
            document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
    }
}
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
